﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using CoffeeMobile.Models;

namespace CoffeeMobile.Controllers
{
    public class TableGroupsController : BaseController
    {
        //
        // GET: /TableGroups/

        public ActionResult Index()
        {
            ViewBag.Title = "Danh mục khu vực";
            ViewBag.TableGroups = _dbLinq.TableGroups.Where(p => p.IsDeleted == false).ToList();
            return View();
        }

        //
        // GET: /TableGroups/Details/5

        public ActionResult Details(int id)
        {
            return View();
        }

        //
        // GET: /TableGroups/Create

        public ActionResult Create()
        {
            AddNewTableGroupModel model = new AddNewTableGroupModel();
            return View(model);
        }

        //
        // POST: /TableGroups/Create

        [HttpPost]
        public ActionResult Create(AddNewTableGroupModel model)
        {

            try
            {
                TableGroup entity = _dbLinq.TableGroups.Where(p => p.Name == model.Name).FirstOrDefault();
                if (entity == null)
                {
                    // TODO: Add insert logic here
                    entity = new TableGroup();
                    entity.ID = Guid.NewGuid();
                    entity.Name = model.Name;
                    entity.CreateDate = DateTime.Now;
                    _dbLinq.TableGroups.InsertOnSubmit(entity);

                }
                else {
                    entity.IsDeleted = false;
                }
                _dbLinq.SubmitChanges();
                return RedirectToAction("Index");
            }
            catch
            {
                return View(model);
            }
        }

        //
        // GET: /TableGroups/Edit/5

        public ActionResult Edit(Guid id)
        {

            EditTableGroupModel entity = new EditTableGroupModel();
            entity = _dbLinq.TableGroups.Where(p => p.ID == id).Select(p => new EditTableGroupModel
            {
                ID = p.ID,
                Name = p.Name
            }).FirstOrDefault();
            return View(entity);
        }

        //
        // POST: /TableGroups/Edit/5

        [HttpPost]
        public ActionResult Edit(Guid id, EditTableGroupModel model)
        {
            try
            {
                // TODO: Add update logic here
                TableGroup entity = _dbLinq.TableGroups.Where(p => p.ID == id).FirstOrDefault();
                if (entity != null)
                {
                    entity.Name = model.Name;
                    _dbLinq.SubmitChanges();
                    return RedirectToAction("Index");
                }
                return View(model);
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /TableGroups/Delete/5

        public ActionResult Delete(Guid id)
        {
            //var tbList = _dbLinq.Tables.Where(p => p.TableGroupID == id).ToList();
            //if (tbList.Count < 1)
            //{
            DeleteTableGroupModel entity = _dbLinq.TableGroups.Where(p => p.ID == id).Select(p => new DeleteTableGroupModel
            {
                ID = p.ID,
                Name = p.Name
            }).FirstOrDefault();
            return View(entity);
            //}
            //ViewBag.ErrorMsg = "Bạn ";
            //    return View(entity);
        }

        //
        // POST: /TableGroups/Delete/5

        [HttpPost]
        public ActionResult Delete(Guid id, DeleteTableGroupModel model)
        {
            try
            {
                // TODO: Add delete logic here
                TableGroup entity = _dbLinq.TableGroups.Where(p => p.ID == id).FirstOrDefault();
                entity.IsDeleted = true;
                _dbLinq.SubmitChanges();
                return RedirectToAction("Index");
            }
            catch
            {
                return View(model);
            }
        }
    }
}
